// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_ok.java

package purchase.apply;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;

public class register_ok extends HttpServlet
{

	public register_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			nseer_db_backup1 purchase_db = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 stock_db = new nseer_db_backup1(dbApplication);
			if (purchase_db.conn((String)dbSession.getAttribute("unit_db_name")) && stock_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String apply_ID = "";
				String product_ID[] = request.getParameterValues("product_ID");
				String product_name[] = request.getParameterValues("product_name");
				String product_describe[] = request.getParameterValues("product_describe");
				String amount[] = request.getParameterValues("amount");
				String pay_ID_group[] = request.getParameterValues("pay_ID_group");
				String designer = request.getParameter("designer");
				String register = request.getParameter("register");
				String register_time = request.getParameter("register_time");
				String bodyb = new String(request.getParameter("remark").getBytes("UTF-8"), "UTF-8");
				String remark = exchange.toHtml(bodyb);
				double demand_amount = 0.0D;
				int n = 0;
				for (int i = 0; i < product_ID.length; i++)
				{
					for (StringTokenizer tokenTO1 = new StringTokenizer(pay_ID_group[i], ", "); tokenTO1.hasMoreTokens();)
					{
						String sql5 = (new StringBuilder("select * from stock_pay where purchase_apply_tag='1' and pay_ID='")).append(tokenTO1.nextToken()).append("'").toString();
						ResultSet rs5 = stock_db.executeQuery(sql5);
						if (rs5.next())
							n++;
					}

					demand_amount += Double.parseDouble(amount[i]);
				}

				if (n == 0)
				{
					String sql;
					if (demand_amount != 0.0D)
					{
						for (int i = 0; i < product_ID.length; i++)
						{
							String sql4;
							for (StringTokenizer tokenTO = new StringTokenizer(pay_ID_group[i], ", "); tokenTO.hasMoreTokens(); stock_db.executeUpdate(sql4))
								sql4 = (new StringBuilder("update stock_pay set purchase_apply_tag='1' where pay_ID='")).append(tokenTO.nextToken()).append("'").toString();

						}

						apply_ID = NseerId.getId(getClass().getResource("").toString(), (String)dbSession.getAttribute("unit_db_name"));
						for (int i = 0; i < product_ID.length; i++)
						{
							sql = (new StringBuilder("insert into purchase_apply(apply_ID,product_ID,product_name,product_describe,amount,pay_ID_group,designer,remark,register,register_time) values('")).append(apply_ID).append("','").append(product_ID[i]).append("','").append(product_name[i]).append("','").append(product_describe[i]).append("','").append(amount[i]).append("','").append(pay_ID_group[i]).append("','").append(designer).append("','").append(remark).append("','").append(register).append("','").append(register_time).append("')").toString();
							purchase_db.executeUpdate(sql);
						}

						response.sendRedirect("purchase/apply/register_ok_a.jsp");
					} else
					{
						for (int i = 0; i < product_ID.length; i++)
						{
							String sql4;
							for (StringTokenizer tokenTO = new StringTokenizer(pay_ID_group[i], ", "); tokenTO.hasMoreTokens(); stock_db.executeUpdate(sql4))
								sql4 = (new StringBuilder("update stock_pay set purchase_apply_tag='1' where pay_ID='")).append(tokenTO.nextToken()).append("'").toString();

						}

						response.sendRedirect("purchase/apply/register_ok_b.jsp");
					}
					List rsList = GetWorkflow.getList(purchase_db, "purchase_config_workflow", "05");
					tokenTO = "";
					if (rsList.size() == 0)
					{
						tokenTO = (new StringBuilder("update purchase_apply set check_tag='1' where apply_ID='")).append(apply_ID).append("'").toString();
						purchase_db.executeUpdate(tokenTO);
					} else
					{
						for (Iterator ite = rsList.iterator(); ite.hasNext(); purchase_db.executeUpdate(tokenTO))
						{
							String elem[] = (String[])ite.next();
							tokenTO = (new StringBuilder("insert into purchase_workflow(config_id,object_ID,describe1,describe2) values ('")).append(elem[0]).append("','").append(apply_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("')").toString();
						}

					}
				} else
				{
					response.sendRedirect("purchase/apply/register_ok_c.jsp");
				}
				stock_db.commit();
				purchase_db.commit();
				stock_db.close();
				purchase_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
